package com.example.dragimage;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.FrameLayout;

/**
 * Created by zf on 2018/7/13.
 */
public class MyFraneLayout extends FrameLayout {
    private static final String TAG = "MyFraneLayout";
    private ViewDragHelper mDragHelper;
    private View targetView;

    private View viewTop, viewBottom;

    private int initTop;
    private int initLeft;

    private OnFinshActivityListenet mActivityListenet;

    private int maxFlingVel;

    public MyFraneLayout(@NonNull Context context) {
        this(context, null);
    }

    public MyFraneLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyFraneLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        setBackgroundColor(ContextCompat.getColor(context, android.R.color.black));

        maxFlingVel = ViewConfiguration.get(context).getScaledMaximumFlingVelocity();

        mDragHelper = ViewDragHelper.create(this, 1.0f, new ViewDragHelper.Callback() {
            boolean isRelease = false;
            int temp;

            @Override
            public boolean tryCaptureView(@NonNull View child, int pointerId) {

                if (initTop == 0) {
                    initTop = targetView.getTop();
                }

                if (initLeft == 0) {
                    initLeft = targetView.getLeft();
                }

                return targetView == child;
            }

            @Override
            public int clampViewPositionVertical(@NonNull View child, int top, int dy) {

                //return Math.max(Math.min(top, getHeight() - targetView.getHeight()), 0);
                return Math.max(Math.min(top, getHeight()), 0); //顶部为0

                /*if (dy < 0) {
                    //不可上移动
                    return child.getTop();
                }
                return Math.max(top, 0);*/
            }

            @Override
            public void onViewPositionChanged(@NonNull View changedView, int left, int top, int dx, int dy) {

                //float mAlpha = (float) top / (getHeight() - changedView.getHeight() + dy);
                //getBackground().mutate().setAlpha((int) ((mAlpha) * 255));

                //view.getHierght() 150

                //Log.i(TAG, "onViewPositionChanged: " + "----->" + dy + "---->" + (changedView.getHeight()) + "----->" + changedView.getTop() + "----->" + changedView.getBottom() + "---->" + getHeight());

                float mAlpha = Math.abs(top - changedView.getHeight() + dy) * 1.0f / getHeight();
                getBackground().mutate().setAlpha((int) (255 * (1 - mAlpha)));

                int tempY = dy;

                if (top == initTop) {
                    /*viewTop.setTranslationY(0);
                    viewBottom.setTranslationY(0);*/

                    isRelease = false;
                    temp = 0;

                } else {

                    //if (viewTop.getTranslationY() == 0) {
                    if (isRelease) {
                        return;
                    }

                    temp += tempY;

                    int height = Math.abs(temp);


                    Log.i(TAG, "onViewPositionChanged: " + height + "---->" + dy);

                    //dy < 0 向上       dy>0  向下
                    if (dy > 0) {
                        viewBottom.setTranslationY(Math.min(height, viewBottom.getHeight()));
                        viewTop.setTranslationY(-Math.min(height, viewTop.getHeight()));

                    } else {

                       /* int w = height;
                        viewBottom.setTranslationY(Math.min(0, -w));
                        viewTop.setTranslationY(Math.max(0, height));*/
                    }
                }
            }

            @Override
            public void onViewReleased(@NonNull View releasedChild, float xvel, float yvel) {

                int top = releasedChild.getTop();

                if (top >= getHeight() - releasedChild.getHeight()) {
                    if (mActivityListenet != null) {
                        //mActivityListenet.onFinshActivityListenet();
                    }
                } else {

                    isRelease = true;

                    mDragHelper.settleCapturedViewAt(initLeft, initTop);

                    viewBottom.animate().translationY(0).setDuration(100).start();
                    viewTop.animate().translationY(0).setDuration(100).start();
                }

                invalidate();
            }
        });
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        targetView = findViewById(R.id.imageView);

        viewTop = findViewById(R.id.viewTop);
        viewBottom = findViewById(R.id.viewBottom);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mDragHelper.processTouchEvent(event);
        return true;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return mDragHelper.shouldInterceptTouchEvent(ev);
        //return super.onInterceptTouchEvent(ev);
    }

    @Override
    public void computeScroll() {
        if (mDragHelper.continueSettling(true)) {
            invalidate();
            postInvalidate();
        }
    }

    public void setmActivityListenet(OnFinshActivityListenet mActivityListenet) {
        this.mActivityListenet = mActivityListenet;
    }

    public interface OnFinshActivityListenet {
        void onFinshActivityListenet();
    }
}
